package com.ycz.sas.jgen.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.Map;

import javax.sql.DataSource;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kingdee.bos.util.CryptException;
import com.ycz.sas.jgen.connection.LocalContextHolder;
import com.ycz.sas.jgen.dao.JdbcDAO;
import com.ycz.sas.jgen.domain.Password;
import com.ycz.sas.jgen.manager.UserManager;
import com.ycz.sas.jgen.service.UserService;

public class TrackerUI extends JDialog {
	
	private UserService userService;

	private JTextField txtUser;
	private JTextField txtPrelike;
	private JTextField txtAftlike;
	private JTextField txtCrackResult;
	
	private JCheckBox chkDigit;
	private JCheckBox chkChar;
	
	private DBConfigPanel pnlDbCfg;

	public TrackerUI(JFrame owner, boolean modal){
		super(owner, modal);
		setSize(430, 250);
		setLocation(400, 300);
		
		initLayout();
	}
	
	@Override
	public String getTitle() {
		return "PWD Tracker";
	}
	
	private void initLayout(){
		setResizable(false);
		JTabbedPane tabPane = new JTabbedPane();
		getContentPane().add(tabPane,"Center");
		
		pnlDbCfg = new DBConfigPanel();
		JPanel pnlCrack = new JPanel();
		tabPane.add(pnlCrack);
		tabPane.add(pnlDbCfg);
		tabPane.setTitleAt(tabPane.indexOfComponent(pnlCrack), "PWD Crack");
		tabPane.setTitleAt(tabPane.indexOfComponent(pnlDbCfg), "DB Config");
		tabPane.setSelectedComponent(pnlCrack);
		
		JLabel contUser = new JLabel("User      ");
		JLabel contPrelike = new JLabel("Pre        ");
		JLabel contAftlike = new JLabel("After      ");
		JLabel contDigit = new JLabel("Digit  ");
		JLabel contChar = new JLabel("Char  ");
		JLabel contResult = new JLabel("Output   ");
		
		txtUser = new JTextField(30);
		txtPrelike = new JTextField(30);
		txtAftlike = new JTextField(30);
		txtCrackResult = new JTextField(30);
		txtCrackResult.setEditable(false);
		JButton btnRun = new JButton("crack");
		chkDigit = new JCheckBox();
		chkChar = new JCheckBox();
		pnlCrack.add(contUser);
		pnlCrack.add(txtUser);
		pnlCrack.add(contPrelike);
		pnlCrack.add(txtPrelike);
		pnlCrack.add(contAftlike);
		pnlCrack.add(txtAftlike);
		pnlCrack.add(contResult);
		pnlCrack.add(txtCrackResult);
		pnlCrack.add(contDigit);
		pnlCrack.add(chkDigit);
		pnlCrack.add(contChar);
		pnlCrack.add(chkChar);
		pnlCrack.add(btnRun);
		

		btnRun.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				try {
					actionCrack_Performed(e);
				} catch (CryptException cex) {
					JOptionPane.showMessageDialog(TrackerUI.this, "Crack Failed");
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(TrackerUI.this, ex.getMessage());
				}
			}
		});
	}
	
	protected void actionCrack_Performed(ActionEvent e) throws Exception{
		initService();
		crack();
	}
	
	private void initService(){
		AbstractApplicationContext context = new ClassPathXmlApplicationContext("Spring_Module.xml");
		System.out.println(LocalContextHolder.getCustomerType());
		userService = (UserService) context.getBean("userService");
	}
	
	private void crack() throws Exception{
		
		String userName = txtUser.getText();
		
		Map<String, String> userInfo = userService.getUserInfo(userName);
		
		String userid = userInfo.get("id");
		String smd5 = userInfo.get("password");
		
		if(userid == null){
			throw new Exception("User not exist!");
		}
		
		final boolean hasDigit = chkDigit.isSelected();
		final boolean hasChar = chkChar.isSelected();
		char[] dict;
		if(hasDigit && hasChar)
			dict = Password.element2;
		else if(hasDigit && !hasChar)
			dict = Password.element3;
		else if(!hasDigit && hasChar)
			dict = Password.element4;
		else
			dict = null;
		String begin = txtPrelike.getText();
		String end = txtAftlike.getText();
		if(dict == null){
			txtCrackResult.setText(Password.allManual_Type(userid, smd5 , begin));
		}else if(dict == Password.element2){
			txtCrackResult.setText("need about 3 hours");
			txtCrackResult.setText(Password.auto_Type(userid, smd5, dict, begin,end));
		}else if(dict == Password.element3){
			txtCrackResult.setText(Password.auto_Type(userid, smd5, dict, begin,end));
		}else{
			txtCrackResult.setText("need about 30 minutes");
			txtCrackResult.setText(Password.auto_Type(userid, smd5, dict, begin,end));
		}
	}

}
